<template>
  <div class="goodsinfo-container">
    <transition v-on:before-enter="beforeEnter" v-on:enter="enter" v-on:after-enter="afterEnter">
      <div class="ball" v-show="ballflag" ref="ball"></div>
    </transition>

    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <lunbotu-box :lunbotuList="lubotu" :imgname="'src'" :isfull="false"></lunbotu-box>
        </div>
      </div>
    </div>

    <div class="mui-card">
      <div class="mui-card-header">{{goodsinfolist.title}}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>
            <span>
              市场价:&nbsp;&nbsp;
              <del>￥{{goodsinfolist.market_price}}</del>
            </span>
            &nbsp;&nbsp;
            <span>销售价:&nbsp;&nbsp;￥{{goodsinfolist.sell_price}}</span>
          </p>
          <p>
            <span>
              购买数量:
              <nobox @func="getSelectCount" :max="goodsinfolist.stock_quantity"></nobox>
            </span>
          </p>
          <div>
            <mt-button type="primary" size="small">立即购买</mt-button>
            <mt-button type="danger" size="small" @click="addToCart">加入购物车</mt-button>
          </div>
        </div>
      </div>
    </div>

    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号:{{goodsinfolist.goods_no}}</p>
          <p>库存情况:{{goodsinfolist.stock_quantity}}</p>
          <p>上架时间:{{goodsinfolist.add_time | dateFormat}}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="goGoodsDesc(goodsinfolist.id)">图文介绍</mt-button>
        <mt-button type="danger" size="large" plain @click="goGoodsComment(goodsinfolist.id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>
<script>
import lunbotu from "../subcomponents/lunbotu.vue";
import nobox from "../subcomponents/goodsinfo-nobox.vue";
export default {
  data() {
    return {
      id: this.$route.params.id,
      lubotu: [],
      goodsinfolist: {},
      ballflag: false,
      selectedCount: 1
    };
  },
  created() {
    this.getLunBoTu();
    this.getGoodsInfo();
  },
  methods: {
    getLunBoTu() {
      this.axios.get("api/getthumimages/" + this.id).then(response => {
        if (response.data.status === 0) {
          this.lubotu = response.data.message;
        }
      });
    },
    getGoodsInfo() {
      this.axios.get("api/goods/getinfo/" + this.id).then(response => {
        if (response.data.status == 0) {
          this.goodsinfolist = response.data.message[0];
        }
      });
    },
    goGoodsDesc(id) {
      this.$router.push({ name: "goodsDesc", params: { id: this.id } });
    },
    goGoodsComment(id) {
      this.$router.push({ name: "goodsComment", params: { id: this.id } });
    },
    addToCart() {
      this.ballflag = !this.ballflag;
      let goodsInfo = {
        id: this.id,
        count: this.selectedCount,
        price: this.goodsinfolist.sell_price,
        selected: true
      };
      this.$store.commit('addToCar',goodsInfo);
    },
    beforeEnter(el) {
      el.style.transform = "translate(0,0)";
    },
    enter(el, done) {
      el.offsetWidth;
      //获取小球的坐标与徽标的坐标
      const ball = this.$refs.ball.getBoundingClientRect();
      const barge = document.getElementById("barge").getBoundingClientRect();
      const xDist = barge.left - ball.left;
      const yDist = barge.top - ball.top;

      el.style.transform = `translate(${xDist}px,${yDist}px)`;
      el.style.transition = "all 0.5s cubic-bezier(.29,.62,.79,.71)";
      done();
    },
    afterEnter(el) {
      this.ballflag = !this.ballflag;
    },
    getSelectCount(c) {
      this.selectedCount = c;
    }
  },
  components: {
    "lunbotu-box": lunbotu,
    nobox
  }
};
</script>
<style lang="scss">
.goodsinfo-container {
  background-color: #eee;
  overflow: hidden;
  .mui-card-footer {
    display: block;
    button {
      margin: 15px 0;
    }
  }
  .ball {
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background-color: red;
    z-index: 99;
    position: absolute;
    top: 406px;
    left: 142px;
  }
}
</style>